package com.bluesky.service.lesson030.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bluesky.comm.BusinessExceptionUtils;
import com.bluesky.dto.lesson030.Lesson030UserRegisterRequest;
import com.bluesky.mapper.lesson030.Lesson030UserMapper;
import com.bluesky.mq.sender.IMsgSender;
import com.bluesky.po.Lesson030.Lesson030UserPO;
import com.bluesky.service.lesson030.ILesson030UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class Lesson030UserServiceImpl extends ServiceImpl<Lesson030UserMapper, Lesson030UserPO> implements ILesson030UserService {

    @Autowired
    private IMsgSender msgSender;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String register(Lesson030UserRegisterRequest request) {

        Lesson030UserPO userPO = new Lesson030UserPO();
        userPO.setId(IdUtil.fastSimpleUUID());
        userPO.setName(request.getName());

        this.save(userPO);

        //发送用户注册消息
        this.msgSender.send(userPO);

        return userPO.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String registerError(Lesson030UserRegisterRequest request) {

        Lesson030UserPO userPO = new Lesson030UserPO();
        userPO.setId(IdUtil.fastSimpleUUID());
        userPO.setName(request.getName());

        this.save(userPO);

        this.msgSender.send(userPO);

        throw BusinessExceptionUtils.businessException("故意失败，看消息是否投递");

    }
}
